﻿using LXYYY.Ids4o;
using LXYYY.Login;
using LXYYY.Models;
using LXYYY.UserDTO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace LXYYY.AdminLogin
{
    public class LoginService : ApplicationService, IAdminLogin
    {
        private readonly IRepository<UserInfos, int> _repository;
        private IConfiguration _configuration;
        public LoginService(IRepository<UserInfos, int> repository, IConfiguration configuration)
        {
            _repository = repository;
            _configuration = configuration;
        }
        public async Task<TokenDto> GetLogin(string LoginName, string Loginpwd)
        {
            Ids4 ids = new Ids4(_configuration);
            var tokenstr = await ids.GetIdsTolenAsync(LoginName, Loginpwd);
            return new TokenDto()
            {
                state = tokenstr != null ? true : false,
                token = tokenstr
            };
        }

        public async Task<string> InSertAsync(MyUserInformationAddDTO model)
        {
            var list = await _repository.InsertAsync(ObjectMapper.Map<MyUserInformationAddDTO, UserInfos>(model));
            return list == null ? "注册失败" : "注册成功";
        }
        [HttpGet,Route("UserLogin")]
        public async Task<TokenDto> UserLogin(string LoginName, string Loginpwd)
        {
            var list = await _repository.GetListAsync();
            var slist = list.Where(x => x.UserInfoName == LoginName && x.UserInfoPwd == Loginpwd).FirstOrDefault();
            Ids4 ids = new Ids4(_configuration);
            var tokenstr = await ids.GetIdsTolenAsync(LoginName, Loginpwd);
            if (slist!=null)
            {
                return new TokenDto()
                {
                    state = tokenstr != null ? true : false,
                    token = tokenstr
                };
            }
            return new TokenDto()
            {
                state = tokenstr != null ? true : false,
                token = tokenstr
            };
        }
        public async Task<TokenDto> JWTUserLogin(string LoginName, string Loginpwd)
        {
            var list = await _repository.GetListAsync();
            var slist = list.Where(x => x.UserInfoName == LoginName && x.UserInfoPwd == Loginpwd).FirstOrDefault();
            if (slist != null)
            {
                return new TokenDto
                {
                    Msg = "成功",
                    state = true,
                    token = JWT.JwtToten.CreateToken(LoginName)
                };
            }
            else
            {
                return new TokenDto { Msg = "失败", state = false };
            }
        }
    }
}
